const axios = require('axios');

const baseURL = 'http://localhost:3002/api';

async function testMerchantAPI() {
  console.log('🧪 测试商家API...\n');

  try {
    // 测试获取所有商家
    console.log('1. 测试获取所有商家');
    const allMerchants = await axios.get(`${baseURL}/merchants`);
    console.log('✅ 成功获取商家列表');
    console.log(`   商家数量: ${allMerchants.data.data.merchants.length}`);
    console.log(`   分页信息: 第${allMerchants.data.data.pagination.page}页，共${allMerchants.data.data.pagination.totalPages}页\n`);

    // 测试获取商家分类
    console.log('2. 测试获取商家分类');
    const categories = await axios.get(`${baseURL}/merchants/categories`);
    console.log('✅ 成功获取商家分类');
    console.log('   分类列表:');
    categories.data.data.forEach(cat => {
      console.log(`   - ${cat.category}: ${cat.count}家`);
    });
    console.log('');

    // 测试按分类获取商家
    if (categories.data.data.length > 0) {
      const firstCategory = categories.data.data[0].category;
      console.log(`3. 测试按分类获取商家 (${firstCategory})`);
      const categoryMerchants = await axios.get(`${baseURL}/merchants/category/${encodeURIComponent(firstCategory)}`);
      console.log('✅ 成功按分类获取商家');
      console.log(`   该分类商家数量: ${categoryMerchants.data.data.merchants.length}\n`);
    }

    // 测试获取商家详情
    if (allMerchants.data.data.merchants.length > 0) {
      const firstMerchant = allMerchants.data.data.merchants[0];
      console.log(`4. 测试获取商家详情 (ID: ${firstMerchant.id})`);
      const merchantDetail = await axios.get(`${baseURL}/merchants/${firstMerchant.id}`);
      console.log('✅ 成功获取商家详情');
      console.log(`   商家名称: ${merchantDetail.data.data.name}`);
      console.log(`   商家分类: ${merchantDetail.data.data.category}`);
      console.log(`   联系电话: ${merchantDetail.data.data.phone}\n`);
    }

    // 测试搜索商家
    console.log('5. 测试搜索商家');
    const searchResult = await axios.get(`${baseURL}/merchants/search/社区`);
    console.log('✅ 成功搜索商家');
    console.log(`   搜索结果数量: ${searchResult.data.data.merchants.length}\n`);

    console.log('🎉 所有API测试通过！');

  } catch (error) {
    console.error('❌ API测试失败:', error.response?.data || error.message);
  }
}

testMerchantAPI();
